﻿using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// Game helper.
/// </summary>
public class GameHelper 
{
	/// <summary>
	/// Calculates the absolute distance.
	/// </summary>
	/// <returns>The absolute distance.</returns>
	/// <param name="Ax">Ax.</param>
	/// <param name="Ay">Ay.</param>
	/// <param name="Bx">Bx.</param>
	/// <param name="By">By.</param>
	public static int CalculateAbsoluteDistance(int Ax, int Ay, int Bx, int By)
	{
		return Math.Max (Math.Abs (Bx - Ax), Math.Abs (By - Ay));
	}

	/// <summary>
	/// Removes the highlights.
	/// </summary>
	/// <param name="selectedToken">Selected token.</param>
	public static void RemoveHighlights(TokenPiece selectedToken)
	{
		if (selectedToken != null) 
		{
			foreach(BoardPiece piece in selectedToken._possibleMoves)
			{
				piece.ToggleSelectable(false);
			}
		}
	}

	/// <summary>
	/// Finds the friendlies adjacent to Token.
	/// </summary>
	/// <returns>The friendlies adjacent to Token.</returns>
	/// <param name="token">Token.</param>
	/// <param name="friendlyTokens">Friendly tokens.</param>
	public static List<TokenPiece> FindFriendliesAdjacent(GameObject token, List<TokenPiece> friendlyTokens)
	{
		// temporary
		int NUMCOLS = 9;

		List<TokenPiece> friendlies = new List<TokenPiece> ();
		for (int i = -1; i <= 1; ++i) 
		{
			for(int j = -1; j <= 1; ++j)
			{
				if(j == 0 && i == 0)
					continue;
				int offset = i*NUMCOLS+j;
				TokenPiece friendly = friendlyTokens.Find(p => p.BoardPosition == token.GetComponent<TokenPiece>().BoardPosition+offset);
				if(friendly != null)
					friendlies.Add(friendly);
			}
		}
		return friendlies;
	}
}
